/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AppServices;

import Facades.CityFacadeRemote;
import Facades.EnterpriseFacadeRemote;
import Facades.LocationFacade;
import Facades.LocationFacadeRemote;
import VOs.Location;
import VOs.City;
import VOs.Enterprise;
import VOs.Plan;
import VOs.Users;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
/**
 *
 * @author rene
 */
public class LocationManage {

     static public LocationFacadeRemote StartLocationContext() throws NamingException{
        InitialContext ctx = new InitialContext();
        LocationFacadeRemote lbeanremote = (LocationFacadeRemote) ctx.lookup("Facades.LocationFacadeRemote");
        return lbeanremote;
    }

     static public CityFacadeRemote StartCityContext() throws NamingException{
        InitialContext ctx = new InitialContext();
        CityFacadeRemote cbeanremote = (CityFacadeRemote) ctx.lookup("Facades.CityFacadeRemote");
        return cbeanremote;
    }

     static public EnterpriseFacadeRemote StartEnterpriseContext() throws NamingException{
        InitialContext ctx = new InitialContext();
        EnterpriseFacadeRemote ebeanremote = (EnterpriseFacadeRemote) ctx.lookup("Facades.EnterpriseFacadeRemote");
        return ebeanremote;
    }



     static public boolean createLocation(String Address, int City, String Enterprise) throws Exception {
        boolean result = false;

        try {
            City c=new City();
            c=StartCityContext().findid(City);
            Enterprise e= new Enterprise();
            e=StartEnterpriseContext().findid(Enterprise);


            int id = StartLocationContext().count();
            id++;
            Location l = new Location();
            l.setId(BigDecimal.valueOf(id));
            l.setAddress(Address);
            l.setFkCity(c);
          //  l.setFkCity(City);
            l.setFkEnterprise(e);
           
            //InitialContext ctx1 = new InitialContext();
            //UsersFacadeRemote ubean = (UsersFacadeRemote) ctx1.lookup("Facades.UsersFacadeRemote");


            StartLocationContext().create(l);

            result = true;


        } catch (Exception ex) {
            result = false;
            throw ex;
        }

        return result;
    }

     static public boolean editLocation(int ID, String Address, int City) throws Exception {
        boolean result = false;

        Location lbdnew = new Location();


        try {
            //InitialContext ctx1 = new InitialContext();
            //InitialContext ctx1 = (InitialContext) a;
            //UsersFacadeRemote ubean = (UsersFacadeRemote) ctx1.lookup("Facades.UsersFacadeRemote");
            lbdnew=StartLocationContext().findid(ID);
            City c=new City();
            c=StartCityContext().findid(City);
            Enterprise e= new Enterprise();
            //e=StartEnterpriseContext().findid(Enterprise);


            //Location l = new Location();
            lbdnew.setAddress(Address);
            lbdnew.setFkCity(c);
            //lbdnew.setFkEnterprise(e);
            //lbdnew.setId(BigDecimal.valueOf(ID));

            StartLocationContext().edit(lbdnew);
            result = true;

        } catch (Exception ex) {
            result = false;
            throw ex;
        }

        return result;
    }

     static public List<Location> getLocation (String Enterprise) throws Exception{
      //List <Location> location;

      try{
     List<Location> l = StartLocationContext().findAll();
            ArrayList<Location> lfin = new ArrayList();

            for (int i =0;i<l.size();i++){
                //System.out.println(r.get(i).getFkPlan().getIdPlan().equals(BigDecimal.valueOf(id)));
                if(l.get(i).getFkEnterprise().getId().equals(Enterprise)){
                lfin.add(l.get(i));
                }
                 }
            return  lfin;
      }
     catch (Exception ex){
     throw ex;
     }
      //return location;
     }

     static public List<Location> getLocationCity (int idCity) throws Exception{
      //List <Location> location;

      try{
     List<Location> lc = StartLocationContext().findAll();
            ArrayList<Location> lcfin = new ArrayList();

            for (int i =0;i<lc.size();i++){
                
                //System.out.println(r.get(i).getFkPlan().getIdPlan().equals(BigDecimal.valueOf(id)));
                if(lc.get(i).getFkCity().getId().equals(BigDecimal.valueOf(idCity))){
                lcfin.add(lc.get(i));
                System.out.println(lcfin);
              
                }
                 }
            return  lcfin;

      }
     catch (Exception ex){
     throw ex;
     }
      //return location;
     }

     static public List<Location> getLocationByTipeEnterprise (String idTipe) throws Exception{
      //List <Location> location;

      try{
          List<Enterprise> lee = EnterpriseManage.getByType(idTipe,0 , 0);
     List<Location> le;
            ArrayList<Location> lefin = new ArrayList();

            for (int i =0;i<lee.size();i++){
                le=getLocation(lee.get(i).getId());
                for(int j=0;j<le.size();j++)
                    lefin.add(le.get(j));
                //System.out.println(r.get(i).getFkPlan().getIdPlan().equals(BigDecimal.valueOf(id)));
                //if(le.get(i).getFkCity().getId().equals(idCity))
                {
                //lefin.add(le.get(i));
                }
                 }
            return  lefin;
      }
     catch (Exception ex){
     throw ex;
     }
      //return location;
     }
}


